/**
 * 
 */
package travibot.engine;

import java.util.HashMap;
import java.util.Map;
import java.util.Observable;
import java.util.Observer;

import org.apache.log4j.Logger;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import travibot.core.ParserFactory;
import travibot.meta.MetabaseManager;
import travibot.utility.RequestDelayGenarator;

/**
 * This is the main entry point to the game session. It takes care of the
 * loading of properties, building the Village(s) from the pages, events
 * management, etc. Functions as a facade to the lower level classes but should
 * expose them whenever needed.
 * 
 * @author Petr Macek
 * 
 */
public class GameSessionFacade implements Observer, ApplicationContextAware {

    private static Logger log = Logger.getLogger(GameSessionFacade.class);
    
    private Map<String, Session> sessionPool = new HashMap<String, Session>();
    
    private MetabaseManager metabaseManager;
    
    private RequestDelayGenarator requestDelayGenerator;
    
    private ParserFactory parserFactory;
    
    private ApplicationContext ctx;
        
    /**
     * Method
     * @param key
     * @param profile
     * @return
     */
    public Session getSession(final String key, final UserProfile profile){
        Session session = new Session(profile, requestDelayGenerator, parserFactory, ctx);
        session.addObserver(this);
        new Thread(session).start();
        sessionPool.put(key, session);
        return session;
    }
    
    public void endSession(final String key){
        Session sess = sessionPool.get(key);
        sess.disconnect();
    }
    
    public void endSession(Session session){
        session.disconnect();
    }


    public void update(Observable arg0, Object arg1) {

        log.info("Session has changed state to: " + arg1);
    }

	public MetabaseManager getMetabaseManager() {
		return metabaseManager;
	}

	public void setMetabaseManager(MetabaseManager metabaseManager) {
		this.metabaseManager = metabaseManager;
	}

    
    public RequestDelayGenarator getRequestDelayGenerator() {
    
        return requestDelayGenerator;
    }

    
    public void setRequestDelayGenerator(RequestDelayGenarator requestDelayGenerator) {
    
        this.requestDelayGenerator = requestDelayGenerator;
    }

    
    public void setParserFactory(ParserFactory parserFactory) {
    
        this.parserFactory = parserFactory;
    }

    public void setApplicationContext(ApplicationContext ctx) throws BeansException {

        this.ctx = ctx; 
    }
    
    
	
	
    
    

}
